﻿using FarPoint.Web.Spread;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.UnboundMode
{
  public partial class Overview : SpreadDemoPage
  {
    Border border = new Border();
    StyleInfo style = new StyleInfo();

    protected void Page_Load(object sender, System.EventArgs e)
    {
      if (!Page.IsPostBack)
      {
        PopulateSpread();
        InitConfirmSpread();
        this.FpSpread1.UseClipboard = false;
      }
    }

    protected override bool ApplyGlobalSkin
    {
      get
      {
        return false;
      }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.FpSpread1.UpdateCommand += new FarPoint.Web.Spread.SpreadCommandEventHandler(this.FpSpread1_UpdateCommand);

    }
    #endregion

    public void PopulateSpread()
    {
      FarPoint.Web.Spread.Model.DefaultSheetDataModel model = new FarPoint.Web.Spread.Model.DefaultSheetDataModel(16, 4); //rows, columns
      FpSpread1.EnableAjaxCall = false;

      FpSpread1.Sheets[0].DataModel = model;
      FpSpread1.Sheets[0].PageSize = 25;
      FpSpread1.Sheets[0].AllowPage = false;

      //Set Column Widths
      FpSpread1.Sheets[0].SetColumnWidth(0, 125);
      FpSpread1.Sheets[0].SetColumnWidth(1, 80);
      FpSpread1.Sheets[0].SetColumnWidth(2, 80);
      FpSpread1.Sheets[0].SetColumnWidth(3, 80);
      //Set Row Height
      FpSpread1.Sheets[0].SetRowHeight(15, 70);

      FpSpread1.Sheets[0].ColumnHeader.Visible = false;
      FpSpread1.Sheets[0].RowHeader.Visible = false;
      FpSpread1.Sheets[0].GridLines = GridLines.None;
      FpSpread1.HorizontalScrollBarPolicy = ScrollBarPolicy.AsNeeded;
      FpSpread1.VerticalScrollBarPolicy = ScrollBarPolicy.AsNeeded;

      FpSpread2.HorizontalScrollBarPolicy = ScrollBarPolicy.Never;
      FpSpread2.VerticalScrollBarPolicy = ScrollBarPolicy.Never;

      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.Font.Size = FontUnit.Parse(10.ToString());
      style1.Font.Name = "Verdana";
      FpSpread1.Sheets[0].SetStyleInfo(-1, -1, style1);

      model.SetValue(0, 1, "联系方式");
      FpSpread1.Sheets[0].SpanModel.Add(0, 1, 1, 3);
      SetHeaderStyle(0, -1);

      model.SetValue(1, 0, "姓名:");
      FpSpread1.Sheets[0].SpanModel.Add(1, 2, 1, 2);
      SetHeaderStyle(1, 0);

      model.SetValue(2, 0, "公司:");
      FpSpread1.Sheets[0].SpanModel.Add(2, 1, 1, 3);
      SetHeaderStyle(2, 0);

      model.SetValue(3, 0, "地址:");
      FpSpread1.Sheets[0].SpanModel.Add(3, 1, 1, 3);
      SetHeaderStyle(3, 0);

      model.SetValue(4, 0, "详细地址:");
      SetHeaderStyle(4, 0);

      model.SetValue(5, 0, "电话:");
      FpSpread1.Sheets[0].SpanModel.Add(5, 1, 1, 3);
      SetHeaderStyle(5, 0);

      //Lock Cells
      LockRow(6);

      model.SetValue(7, 1, "礼品 ");
      FpSpread1.Sheets[0].SpanModel.Add(7, 1, 1, 3);
      SetHeaderStyle(7, -1);

      model.SetValue(8, 0, "这是一个礼物");
      FpSpread1.Sheets[0].SpanModel.Add(8, 1, 1, 3);
      SetHeaderStyle(8, 0);

      //Lock Cells
      LockRow(9);

      model.SetValue(10, 1, "货船");
      SetHeaderStyle(10, -1);

      model.SetValue(11, 0, "货运途径");
      FpSpread1.Sheets[0].SpanModel.Add(11, 1, 1, 3);
      SetHeaderStyle(11, 0);

      model.SetValue(12, 0, "服务");
      FpSpread1.Sheets[0].SpanModel.Add(12, 1, 1, 3);
      SetHeaderStyle(12, 0);

      //Lock Cells
      LockRow(13);

      model.SetValue(14, 1, "付款");
      SetHeaderStyle(14, -1);

      model.SetValue(15, 0, "选择付款方式");
      FpSpread1.Sheets[0].SpanModel.Add(15, 1, 1, 3);
      SetHeaderStyle(15, 0);

      //Styles
      style1 = new FarPoint.Web.Spread.StyleInfo();
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(1, 1, style1); // First Name
      FpSpread1.ActiveSheetView.SetStyleInfo(1, 2, style1); //Last Name
      FpSpread1.ActiveSheetView.SetStyleInfo(2, 1, style1); //Company
      FpSpread1.ActiveSheetView.SetStyleInfo(3, 1, style1); //Address
      FpSpread1.ActiveSheetView.SetStyleInfo(4, 1, style1); //City
      FpSpread1.ActiveSheetView.SetStyleInfo(4, 2, style1); //State
      FpSpread1.ActiveSheetView.SetStyleInfo(4, 3, style1); //Zip
      FpSpread1.ActiveSheetView.SetStyleInfo(5, 1, style1); //Phone

      // Image cell        
      //Contact
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new ImageCellType("../images/contactinfo.gif");
      style1.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(0, 0, style1);

      //Gift
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new ImageCellType("../images/giftsm.gif");
      style1.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(7, 0, style1);

      //Shipping
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new ImageCellType("../images/globe.gif");
      style1.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(10, 0, style1);

      //Payment
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new ImageCellType("../images/creditcard.jpg");
      style1.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(14, 0, style1);

      //Gift
      //Checkbox
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new CheckBoxCellType();
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(8, 1, style1);

      //Ship via
      // ComboBox
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new ComboBoxCellType(new string[] { "FedEx", "UPS", "Other" }, new string[] { "FedEx", "UPS", "Other" });
      style1.Font.Size = FontUnit.Parse(10.ToString());
      style1.Font.Name = "Verdana";
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(11, 1, style1);

      //Service
      //Radio
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new RadioButtonListCellType(new string[] { "Ground", "2-Day", "Overnight" }, new string[] { "Ground", "2-Day", "Overnight" });
      style1.Font.Size = FontUnit.XSmall;
      style1.Font.Name = "Verdana";
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(12, 1, style1);
      FpSpread1.ActiveSheetView.SetRowHeight(12, 35);

      //Payment
      // ListBox cell              
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new ListBoxCellType(new string[] { "Visa", "MC", "AMEX" }, new string[] { "Visa", "MC", "AMEX" });
      style1.Font.Size = FontUnit.Parse(10.ToString());
      style1.Font.Name = "Verdana";
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(15, 1, style1);
    }

    public void SetHeaderStyle(int row, int col)
    {
      //Header Column
      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.CellType = new LabelCellType();

      style1.BackColor = Color.LightSlateGray;
      style1.ForeColor = Color.White;

      if (col == 0)
        style1.HorizontalAlign = HorizontalAlign.Right;
      else
      {
        style1.Font.Size = FontUnit.Parse(10.ToString());
        style1.Font.Bold = true;
        style1.ForeColor = Color.Wheat;
      }

      FpSpread1.Sheets[0].SetStyleInfo(row, col, style1);
    }

    public void LockRow(int row)
    {
      //Span Cells
      FpSpread1.Sheets[0].SpanModel.Add(row, 0, 1, 4);

      style1 = new FarPoint.Web.Spread.StyleInfo();
      style1.Locked = true;
      FpSpread1.ActiveSheetView.SetStyleInfo(row, -1, style1);
    }

    public void AddBorder()
    {
      //Add border around the cell
      border1 = (Border)border1.Clone();
      border1.BorderStyle = BorderStyle.Solid;
      border1.BorderColor = Color.Silver;
      border1.BorderSize = 1;
      style1.Border = border1;
    }

    //public void InitConfirmSpread()
    //{
    //  //Set the paging size
    //  FpSpread2.Sheets[0].PageSize = 12;

    //  FarPoint.Web.Spread.Model.ISheetDataModel model;
    //  model = new FarPoint.Web.Spread.Model.DefaultSheetDataModel(12, 2);

    //  //Add text
    //  model.SetValue(0, 0, "First Name:");
    //  model.SetValue(1, 0, "Last Name:");
    //  model.SetValue(2, 0, "Company:");
    //  model.SetValue(3, 0, "Address:");
    //  model.SetValue(4, 0, "City:");
    //  model.SetValue(5, 0, "State:");
    //  model.SetValue(6, 0, "Zip:");
    //  model.SetValue(7, 0, "Phone:");
    //  model.SetValue(8, 0, "Gift?:");
    //  model.SetValue(9, 0, "Ship Via:");
    //  model.SetValue(10, 0, "Service:");
    //  model.SetValue(11, 0, "Payment:");

    //  //Apply data to Spread
    //  FpSpread2.Sheets[0].DataModel = model;

    //  //Hide the command bar
    //  FpSpread2.CommandBar.Visible = false;

    //  //Set header text
    //  FpSpread2.Sheets[0].ColumnHeaderDataModel.SetValue(0, 0, "Field");
    //  FpSpread2.Sheets[0].ColumnHeaderDataModel.SetValue(0, 1, "Value");

    //  FpSpread2.Sheets[0].RowHeader.Visible = false;  //Hide row headers

    //  //Set up columns
    //  FpSpread2.Sheets[0].Columns[0].Font.Name = "Verdana";
    //  FpSpread2.Sheets[0].Columns[0].Font.Size = FontUnit.XSmall;
    //  FpSpread2.Sheets[0].Columns[0].Font.Italic = true;
    //  FpSpread2.Sheets[0].Columns[0].HorizontalAlign = HorizontalAlign.Right;
    //  FpSpread2.Sheets[0].Columns[0].BackColor = Color.Linen;
    //  FpSpread2.Sheets[0].Columns[0].Locked = true;   //Lock to prevent editing

    //  FpSpread2.Sheets[0].Columns[1].Font.Name = "Verdana";
    //  FpSpread2.Sheets[0].Columns[1].Font.Size = FontUnit.XSmall;
    //  FpSpread2.Sheets[0].Columns[1].Locked = true;   //Lock to prevent editing

    //  //Turn off border for the header
    //  FpSpread2.Sheets[0].ColumnHeader.DefaultStyle.Border.BorderStyle = BorderStyle.None;

    //}

    private void FpSpread1_UpdateCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
    {
      //Editing values
      //This event fires once for every edited row 
      //   ie: edit three different rows and the event fires three times
      //   ie: edit three columns in the SAME row(one row) and the event fires once

      //Display the changes in fpSpread2 (confirmation Spread)
      DisplayConfirmation(e);
      e.Handled = false;
    }

    public void DisplayConfirmation(FarPoint.Web.Spread.SpreadCommandEventArgs e)
    {
      //Display the changes
      int row;
      FarPoint.Web.Spread.Model.ISheetDataModel model;
      model = FpSpread2.ActiveSheetView.DataModel;

      //Get the current editing row
      row = (int)e.CommandArgument;

      switch (row)
      {
        case 1:  //FirstName , LastName
          AddToConfirmation(1, 0, e, model);
          AddToConfirmation(2, 1, e, model);
          break;
        case 2:  //Company
          AddToConfirmation(1, 2, e, model);
          break;
        case 3:  //Address
          AddToConfirmation(1, 3, e, model);
          break;
        case 4:  //City,State,Zip
          AddToConfirmation(1, 4, e, model);   //City
          AddToConfirmation(2, 5, e, model);   //State
          AddToConfirmation(3, 6, e, model);   //Zip
          break;
        case 5:  //Phone
          AddToConfirmation(1, 7, e, model);
          break;
        case 8:  //Gift
          AddToConfirmation(1, 8, e, model);
          break;
        case 11: //Ship via
          AddToConfirmation(1, 9, e, model);
          break;
        case 12: //Service
          AddToConfirmation(1, 10, e, model);
          break;
        case 15: //Payment
          AddToConfirmation(1, 11, e, model);
          break;
      }

      //Apply data
      FpSpread2.Sheets[0].DataModel = model;
    }

    //public void AddToConfirmation(int sourcecol, int destrow, FarPoint.Web.Spread.SpreadCommandEventArgs e, FarPoint.Web.Spread.Model.ISheetDataModel model)
    ////Add the edited data to the data model
    //{
    //  if (!object.ReferenceEquals(e.EditValues[sourcecol], FarPoint.Web.Spread.FpSpread.Unchanged))
    //    model.SetValue(destrow, 1, e.EditValues[sourcecol]);
    //}
  }
}